var param = {}, formId = 'queryForm', url = "/order/queryService/listPickTime", orderDetail = {};
/**新增*/
var rowindex = 100;
var days = 30;
var orderCode = null;
var intervalDays = 7;

//点击查询按钮调用
$.openx.query = function () {
    try{
        getSystemParam();
    }catch (e) {
        console.info(e)
    }
    var createStartTime = new Date($("#createStartTime").val().replace(/\-/g, "\/"));
    var createEndTime = new Date($("#createEndTime").val().replace(/\-/g, "\/"));
    getDaysByMonth(createStartTime.getFullYear(), createStartTime.getMonth() + 1);
    if (createStartTime > createEndTime) {
        layer.msg('下单开始时间不能大于下单结束时间！');
        return false;
    }
    var ticketPrintedStartTime = new Date($("#ticketPrintedStartTime").val().replace(/\-/g, "\/"));
    var ticketPrintedEndTime = new Date($("#ticketPrintedEndTime").val().replace(/\-/g, "\/"));
    getDaysByMonth(ticketPrintedStartTime.getFullYear(), ticketPrintedStartTime.getMonth() + 1);
    if (ticketPrintedStartTime > ticketPrintedEndTime) {
        layer.msg('拣货开始时间不能大于拣货结束时间！');
        return false;
    }
    createStartTime = new Date(createStartTime.setDate(createStartTime.getDate() + intervalDays));
    ticketPrintedStartTime = new Date(ticketPrintedStartTime.setDate(ticketPrintedStartTime.getDate() + intervalDays));
    var orderCode = $("input[name='orderCode']").val();
    var parentOrderCode = $("input[name='parentOrderCode']").val();
    var outTradeCode = $("input[name='outTradeCode']").val();
    if (orderCode.trim() === '' && parentOrderCode.trim() === '' && outTradeCode.trim() === '') {
        if (isNaN(createStartTime.getTime())) {
            layer.msg('下单开始时间不能为空！');
            return false;
        }
        if (isNaN(ticketPrintedStartTime.getTime())) {
            layer.msg('下单结束时间不能为空！');
            return false;
        }
        if (isNaN(createStartTime.getTime())) {
            layer.msg('拣货开始时间不能为空！');
            return false;
        }
        if (isNaN(ticketPrintedEndTime.getTime())) {
            layer.msg('拣货结束时间不能为空！');
            return false;
        }
        if (createStartTime < createEndTime) {
            layer.msg('选择的时间区间必须在' + intervalDays + '天以内');
            return false;
        }
        if (ticketPrintedStartTime < ticketPrintedEndTime) {
            layer.msg('选择的时间区间必须在' + intervalDays + '天以内');
            return false;
        }
    }
    var data = $.openx.getJsonObj(formId);
    param.order = data;
    var equryTimeStamp = new Date();
    $.openx.tableRefresh(url + "?t=" + equryTimeStamp.getTime(), param);
    $('#table').bootstrapTable({pageList: [10, 25, 50]});
}

function resetForm() {
    $('#queryForm')[0].reset();
    var laydate = layui.laydate;
    console.log(laydate.now().split(' ')[0]);
    document.getElementById('createStartTime').value = laydate.now(-1).split(' ')[0] + ' 00:00:00';
    document.getElementById('createEndTime').value = laydate.now().split(' ')[0] + ' 23:59:59';
    document.getElementById('ticketPrintedStartTime').value = laydate.now(-1).split(' ')[0] + ' 00:00:00';
    document.getElementById('ticketPrintedEndTime').value = laydate.now().split(' ')[0] + ' 23:59:59';

    var data = $.openx.getJsonObj(formId);
    param.order = data;
    $.openx.initTable("table", url, param);
    $.openx.query();
}

//初始化日期控件时间
layui.use('laydate', function () {
    var laydate = layui.laydate;
    if (UrlParameter()['orderCode'] != '' && UrlParameter()['orderCode'] != null) {
        document.getElementById('createStartTime').value = '';
        document.getElementById('createEndTime').value = '';
        document.getElementById('ticketPrintedStartTime').value = '';
        document.getElementById('ticketPrintedEndTime').value = '';
        var data = $.openx.getJsonObj(formId);
        data.orderCode = UrlParameter()['orderCode'];
    } else {
        document.getElementById('createStartTime').value = laydate.now(-1) + ' 00:00:00';
        document.getElementById('createEndTime').value = laydate.now() + ' 23:59:59';
        document.getElementById('ticketPrintedStartTime').value = laydate.now(-1) + ' 00:00:00';
        document.getElementById('ticketPrintedEndTime').value = laydate.now() + ' 23:59:59';
        var data = $.openx.getJsonObj(formId);
    }
    param.order = data;
    console.log(param.order);
    $.openx.initTable("table", url, param, null, [10, 25, 50]);
});

/**
 * 格式化下单时间
 */
function createTimeFormat(value, row) {
    return $.openx.formatDate(new Date(value), "yyyy-MM-dd hh:mm");
}

/**
 * 格式化下单时间
 */
function ticketPrintedTimeFormat(value, row) {
    return $.openx.formatDate(new Date(value), "yyyy-MM-dd hh:mm");
}

function pickTime(value, row) {
    return parseInt((row.ticketPrintedTime - row.createTime)/1000/60);
}

function topChannelFormat(value, row) {
    return channelMap.get(value);
}

function state(value, row) {
    return channelMap.get(value);
}

function getDaysByMonth(year, month) {
    var monthDays;
    //判断月份大小获得天数
    if (month == '1' || month == '3' || month == '5' || month == '7' || month == '8' || month == '10' || month == '12') {
        monthDays = 31;
    } else if (month == '4' || month == '6' || month == '9' || month == '11') {
        monthDays = 30;
    } else {
        monthDays = 28;
        //判断当前选择的年份是否是闰年
        if ((year % 100 == 0 && year % 400 == 0) || year % 4 == 0) {
            monthDays = 29;
        }
    }
    days = monthDays;
    return days;
}

function getSystemParam() {
    ajax(x_jot_sic_server + "/SysParamService", "getSystemParam").callx({
        onResult: function (data) {
            if (data) {
                allowConsortGoBack = data.allowConsortGoBack;
                allowTransferReject = data.allowTransferReject;
            }
        },
        onError: function (error) {
        }
    });
}
